package com.hubiao.nacos.consumer.controller;

import com.hubiao.microservice.service1.service.Service1Api;
import com.hubiao.microservice.service2.service.Service2Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;

/**
 * Description 服务消费方
 *
 * @author hubiao
 * @since 2020-09-24 10:56
 */
@RestController
public class RestConsumerController {

    /**
     * 要想远程调用，必须知道被调用方的Ip + 端口
     */
    @Value("${provider.address}")
    private String providerAddress;
    /**
     * 调用服务名
     **/
    private String serviceId = "nacos-restful-provider";


    //======================================基于RestFull协议使用nacos服务转发调用服务========================================
    /**
     * loadBalancerClient底层使用的是springCloud alibaba框架提供的Ribbon客户端的负载均衡 而Nginx是属于服务端的负载均衡
     **/
    @Resource
    private LoadBalancerClient loadBalancerClient;

    /**通过负载均衡发现地址，流程是从服务中心拿nacos-restful-provider服务的列表，通过负载均衡的算法获取 **/
    /**
     * 通过Reference注解生成Service2Api代理对象，来提供远程调用
     **/
    @org.apache.dubbo.config.annotation.Reference
    private Service2Api service2Api;
    /**
     * 通过Reference注解生成Service1Api代理对象，来提供远程调用
     **/
    @org.apache.dubbo.config.annotation.Reference
    private Service1Api service1Api;

    //======================================基于Dubbo框架的RPC协议使用nacos服务转发调用服务========================================
    //注入配置文件上下文
    @Resource
    private ConfigurableApplicationContext configurableApplicationContext;

    @GetMapping("/service")
    public String service() {
        //远程调用
        RestTemplate restTemplate = new RestTemplate();
        String forObject = restTemplate.getForObject( "http://" + providerAddress + "/service", String.class );
        return "consumer invoke|" + forObject;
    }

    @GetMapping("/service1")
    public String service1() {
        //远程调用
        RestTemplate restTemplate = new RestTemplate();
        //发线一个地址
        ServiceInstance serviceInstance = loadBalancerClient.choose( serviceId );
        //获取http://开头的地址，包括ip+端口
        URI uri = serviceInstance.getUri();
        String forObject = restTemplate.getForObject( uri + "/service", String.class );
        return "consumer invoke|" + forObject;
    }

    @GetMapping("/service2")
    public String service2() {
        //远程调用
        String resultDubbo = service2Api.dubboService();
        return "consumer dubbo invoke|" + resultDubbo;
    }


    //======================================使用nacos搭建配置中心========================================

    @GetMapping("/service3")
    public String service3() {
        //远程调用
        String resultDubbo = service1Api.dubboService1();
        return "consumer dubbo invoke | " + resultDubbo;
    }

    @GetMapping("/configs")
    public String configs() {
        return configurableApplicationContext.getEnvironment().getProperty( "common.name" );
    }

}
